using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Overflow_RTS.Entities;
using System.Diagnostics;
using Overflow_RTS.Utilities;

namespace Overflow_RTS
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        Primitives2d p2D;
        GraphicsDeviceManager graphics;
        SpriteBatch sBatch;

        Rectangle selectionBox;
        MouseState currentMouseState;
        MouseState previousMouseState;
        Entity player;

        SpriteFont font;

        // Pictures Init
        private Texture2D imgPlayer;


        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.IsMouseVisible = true;

            // Create a new SpriteBatch, which can be used to draw textures.
            sBatch = new SpriteBatch(GraphicsDevice);
            p2D = new Primitives2d(GraphicsDevice, sBatch);


            player = new Entity(sBatch, p2D);
            selectionBox = new Rectangle(-1, -1, 0, 0);
            
            // End Initialize
            base.Initialize();        
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {            
            // TODO: use this.Content to load your game content here
            imgPlayer = Content.Load<Texture2D>(player.getImg());

            font = Content.Load<SpriteFont>("Courier New");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            //manageMouse();
            manageMouse();
            player.update(gameTime);

            if (selectionBox.Intersects(player.getHitbox()))
            { player.selected = true; }

            // End Update
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Window Settings
            int screenWidth = Window.ClientBounds.Width;
            int screenHeight = Window.ClientBounds.Height;
            
            // TODO: Add your drawing code here
            base.Draw(gameTime);

            // Begin Drawing Step
            sBatch.Begin();

            //Player
            player.draw(imgPlayer);

            // UI
            sBatch.DrawString(font, " Position : " + player.getPos().X + " , " + player.getPos().Y +
                "\n Destination : " + player.getDestination().X + " , " + player.getDestination().Y + 
                "\n Vel : " + player.getVel() + 
                "  Distance : " + (player.getDestination()-player.getPos()).Length(),
                new Vector2(400, 5), Color.Black,0,Vector2.Zero, 0.55f, SpriteEffects.None, 1);

            p2D.DrawRectangle(selectionBox, new Color(0,90,0,100), true);            
            p2D.DrawRectangle(selectionBox, Color.LightGreen, false);

            //End Drawing Step
            sBatch.End();

        }

        // UI methods - Methods fromh here to the next section are used to manage de UI
        protected void manageMouse()
        {
            currentMouseState = Mouse.GetState();
            if (currentMouseState.RightButton == ButtonState.Pressed)
            {
                player.setVel(0.5f);
                player.setDirection(new Vector2( currentMouseState.X - player.getPos().X, currentMouseState.Y - player.getPos().Y));
                player.setDestination(currentMouseState.X, currentMouseState.Y);
                player.setDepart();
            }

            if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
            {
                selectionBox = new Rectangle(currentMouseState.X, currentMouseState.Y, 0, 0);
            }

            if (currentMouseState.LeftButton == ButtonState.Pressed)
            {
                selectionBox = new Rectangle(selectionBox.X, selectionBox.Y, currentMouseState.X - selectionBox.X, currentMouseState.Y - selectionBox.Y);
            }

            if (currentMouseState.LeftButton == ButtonState.Released)
            {
                selectReset();
                selectionBox = new Rectangle(-1, -1, 0, 0);
            }

            previousMouseState = currentMouseState;
        }

        protected void selectReset()
        {
            player.selected = false;
        }

    }
}
